﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Model.Domain;
using System.Data.Common;
using IMPlant.Core.Data.Unity;
using System.Data;
using SMTC.Model.Model;

namespace SMTC.Dao.Line
{
    public class LineProccessDao : BaseDao
    {
        /// <summary>
        /// 插入生产线信息
        /// </summary>
        private const string SQL_INSERT_LINE = @"INSERT INTO dbo.SMT_Line (
	                                            LineID,
	                                            LineName,
	                                            LineTargetTime,
	                                            BUID,
	                                            ModifyPerson,
	                                            ModifyDateTime,
                                                LineCapacity
                                            ) VALUES ( 
	                                            /* LineID - uniqueidentifier */ @LineID,
	                                            /* LineName - nvarchar(50) */ @LineName,
	                                            /* LineTargetTime - float */ @LineTargetTime,
	                                            /* BUID - uniqueidentifier */ NULL,
	                                            /* ModifyPerson - nvarchar(50) */ @ModifyPerson,
	                                            /* ModifyDateTime - datetime */ GETDATE(),
                                                                                @LineCapacity) ";
        /// <summary>
        /// 跟新贴片机的与生产线的隶属关系
        /// </summary>
        private const string SQL_INSERT_LINE_PRODUCTUNIT = @"UPDATE dbo.SMT_ProductionUnit
                                                            SET LineID = @LineID
                                                            WHERE PUID = @PUID";
        /// <summary>
        /// 删除生产线
        /// </summary>
        private const string SQL_DELETE_LINE = @"DELETE dbo.SMT_Line WHERE LineID = @LineID";
        /// <summary>
        /// 删除生产线的贴片机关系
        /// </summary>
        private const string SQL_DELETE_LINE_PRODUCTUNIT = @"UPDATE dbo.SMT_ProductionUnit SET LineID = NULL
                                                            WHERE LineID = @LineID";
        /// <summary>
        /// 更新生产线信息
        /// </summary>
        private const string SQL_UPDATE_LINE = @"UPDATE dbo.SMT_Line 
                                                SET LineName = @LineName
                                                ,LineTargetTime = @LineTargetTime
                                                ,ModifyPerson = @ModifyPerson
                                                ,ModifyDateTime = @ModifyDateTime
                                                ,LineCapacity = @LineCapacity
                                                WHERE LineID = @LineID";
        /// <summary>
        /// 插入生产线信息
        /// </summary>
        /// <param name="domain"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertLine(SMT_LineModel line, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@LineID",line.LineID)
               ,DBparameterBuild.Build("@LineName",line.LineName)
               ,DBparameterBuild.Build("@LineTargetTime",line.LineTargetTime)
               ,DBparameterBuild.Build("@ModifyPerson",line.ModifyPerson)
               ,DBparameterBuild.Build("@LineCapacity",line.LineCapacity)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_LINE, parameters, dbt) > 0;
        }
        /// <summary>
        /// 更新生产线的贴片机关系
        /// </summary>
        /// <param name="pu"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool InsertLinesProductUnit(SMT_ProductionUnitModel pu, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@LineID",pu.LineID)
               ,DBparameterBuild.Build("@PUID",pu.PUID)

            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_INSERT_LINE_PRODUCTUNIT, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除生产线
        /// </summary>
        /// <param name="LineID"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeleteLine(string LineID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@LineID",LineID)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_LINE, parameters, dbt) > 0;
        }
        /// <summary>
        /// 删除生产线和贴片机关系
        /// </summary>
        /// <param name="LineID"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool DeleteLinesProductUnit(string LineID, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@LineID",LineID)
            };
            try
            {
                DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_DELETE_LINE_PRODUCTUNIT, parameters, dbt);
                return true;
            }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 更新生产线信息
        /// </summary>
        /// <param name="line"></param>
        /// <param name="dbt"></param>
        /// <returns></returns>
        public bool UpdateLine(SMT_LineModel line, ref DbTransaction dbt)
        {
            DbTransaction localDbt = null;
            if (dbt == null)
            {
                dbt = DBTemplete.CreateDbTransaction();
                localDbt = dbt;
            }
            else
            {
                localDbt = dbt;
            }

            DbParameter[] parameters = 
            {
                DBparameterBuild.Build("@LineName",line.LineName)
               ,DBparameterBuild.Build("@LineTargetTime",line.LineTargetTime)
               ,DBparameterBuild.Build("@ModifyPerson",line.ModifyPerson)
               ,DBparameterBuild.Build("@ModifyDateTime",line.ModifyDateTime)
               ,DBparameterBuild.Build("@LineID",line.LineID)
               ,DBparameterBuild.Build("@LineCapacity",line.LineCapacity)
            };

            return DBTemplete.ExcuteNoQuery(CommandType.Text, SQL_UPDATE_LINE, parameters, dbt) > 0;
        }

    }
}
